package com.bawei.library_base.utils;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

/**
 * @author : zhiyu
 * @date : 2021/12/10
 * @content :
 */
public class GlideUtil {

    private static GlideUtil mInstance;

    private GlideUtil() {
    }

    public static GlideUtil getInstance() {
        if (mInstance == null) {
            synchronized (GlideUtil.class) {
                if (mInstance == null) {
                    mInstance = new GlideUtil();
                }
            }
        }
        return mInstance;
    }

    //正常加载图片
    public  void LoadImage(Context context, Object url, ImageView imageView){
        Glide.with(context).load(url).into(imageView);
    }
    //加载指定大小
    public  void loadImageViewSize(Context mContext, Object url, int width, int height, ImageView mImageView) {
        Glide.with(mContext).load(url).override(width, height).into(mImageView);
    }

    //加载全角圆
    public   void loadRoundImage(Context context,Object url,ImageView imageView,int roundingRadius){
        Glide.with(context).load(url)
                .apply(new RequestOptions().transform(new RoundedCorners(roundingRadius)))
                .into(imageView);
    }
    //加载指定圆角
    public   void LoadFilleted(Context context,Object url,ImageView imageView,boolean leftTop,boolean rightTop,boolean rightBottom ,boolean leftBottom,int corner ){

        Glide.with(context).load(url).into(imageView);
    }
    //加载圆
    public   void LoadCircleImage(Context context,Object url,ImageView imageView){
        Glide.with(context).load(url).circleCrop().into(imageView);
    }
    public  void GuideClearDiskCache(Context mContext) {
        //理磁盘缓存 需要在子线程中执行
        Glide.get(mContext).clearDiskCache();
    }
    // 设置加载中以及加载失败图片并且指定大小
    public  void loadImageViewLodingSize(Context mContext, String path, int width, int height, ImageView mImageView, int lodingImage, int errorImageView) {
        Glide.with(mContext).load(path).override(width, height).placeholder(lodingImage).error(errorImageView).into(mImageView);
    }

    //指定角圆角图片

    public  void loadRadImage(Context mContext, String path, ImageView mImageView, int ImgRad, RoundedCornersTransformation.CornerType type) {
        RequestOptions request = new RequestOptions()
                .centerCrop()
                .circleCrop()
                .apply(bitmapTransform(new RoundedCornersTransformation(ImgRad, 0, type)))
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(mContext)
                .load(path)
                .apply(request)
                .into(mImageView);


    }
//    fun loadRadImage(context: Context,url:String,imageView: ImageView,rad:Int,type: RoundedCornersTransformation.CornerType){
//        val request = RequestOptions()
//                .centerCrop()
//                .circleCrop()
//                .apply(bitmapTransform(RoundedCornersTransformation(rad,0,type)))
//                .priority(Priority.HIGH)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//
//        Glide.with(context)
//                .load(url)
//                .apply(request)
//                .into(imageView)
//    }
}


